#!/usr/bin/perl -w

# The setup.* routines will eventually be folded into larger scripts 
# that configure the cluster or an application as a whole.
#
# This one performs the task of populating the 'topology' talb in the
# configuration database on all contactable nodes.
#
# If supports the --reload to populate the topology even if it is
# already populated. 
# 

BEGIN {
	if(!exists($::ENV{TRUECL_CODE})) {
		die("Missing TRUECL_CODE environment variable.");
	}
	if(!exists($::ENV{TRUECL_LOG})) {
		die("Missing TRUECL_LOG environment variable.");
	}
	if(!exists($::ENV{TRUECL_CFG})) {
		die("Missing TRUECL_CFG environment variable.");
	}
	if(! -d "$::ENV{TRUECL_CODE}/clreqd_modules") {
		die("Missing clreqd_modules directory in $::ENV{TRUECL_CODE}.");
	}
	push @::INC,"$::ENV{TRUECL_CODE}/lib";
	$::PKI=1;
	eval "use Crypt::RSA;";
	$::PKI=0 if $@;
}

$::PKI=1;
if($::PKI==1 && exists($::ENV{TRUECL_PKI})) {
	$::PKI=$::ENV{TRUECL_PKI};
}

sub usage {
	print "
Usage: setup.db-config [--force] [--recreate] [--timeout N] [--parallel] [--width N]
";
	exit 1;
}

use strict;
use Getopt::Long;
use common_requests;
use clreqdxml;
use cllogging;
use Data::Dumper;

$::ARG_timeout=10;
$::ARG_force=0;
$::ARG_recreate=0;
$::ARG_width=80;
$::ARG_parallel=0;

if(!GetOptions(
	"timeout=i"		=> \$::ARG_timeout,
	"width=i"		=> \$::ARG_width,
	"parallel"		=> \$::ARG_parallel,
	"reload|recreate"	=> \$::ARG_recreate,
	"force"			=> \$::ARG_force,
)) {
	usage;
}


#########################################################################
# Put in command line argument processing here and a usage msg.		#
#########################################################################

$::LOGGER=new cllogging("-",$::ARG_width);

if(!defined($::LOGGER)) {
	print STDERR "Error: Unable to initialise logging object.\n";
	exit 1;
}

$::LOGGER->set_verbose;
$::CLREQD=new clreqdxml("$::ENV{TRUECL_CFG}/clreqd.xml");
if(!defined($::CLREQD)) {
	$::LOGGER->msg("error","Unable to open/parse file $::ENV{TRUECL_CFG}/clreqd.xml - aborting.");
	$::LOGGER->msg("error","MSG from object: ".clreqdxml->error);
	exit 1;
}
$::LOGGER->msg("msg","Initialised 'cluster request daemon interface' object successfully.");

$::COMMON=new common_requests(timeout=>$::ARG_timeout,parallel=>$::ARG_parallel,clreqdxml=>$::CLREQD);
if(!defined($::COMMON)) {
	$::LOGGER->msg("error","Unable to initialise 'common requests' object.");
	exit 1;
}

$::LOGGER->msg("msg","Initialised 'common requests' object successfully.");


#########################################################################
# At this point we've got to do some real work!!			#
#########################################################################

{
	$::LOGGER->msg("msg","Attempting load of topology configuration.");
	my ($list,$err)=$::COMMON->load_topology(force=>$::ARG_force,recreate=>$::ARG_recreate);

	if(!defined($list)) {
		$::LOGGER->msg("error"," [$err]");
		exit 2;
	}
		
	my $chost;
	my $cport=undef;
	my $errs=0;
	for $chost (sort(keys(%$list))) {
		if(!defined($list->{$chost})) {
			next;
		}
		my $rr=$list->{$chost};
		$::LOGGER->msg("msg","$chost: $rr");
		if($rr =~ /ERROR CONNECT ERROR/) {
			$errs++ if $::ARG_force==0;
			next;
		}
		if($rr =~ /^(error|fail)/i) {
			$errs++;
		}
	}
	exit(1) if $errs;
}

exit(0);
